<?php
defined('APP_PATH') or die ('Unauthorized access');
/**
 * 路由核心类库
 * @author winpurple
 * @version 1.0
 */


final class Router
{
	static private $_instance = NULL;
	
	public function __construct()
	{
		
	}
	
	static function getInstance()
	{
		if(is_null(self::$_instance) || !isset(self::$_instance)){
			self::$_instance = new Router();
		}
		return self::$_instance;
	}
	
	//获取路由控制器和方法
	public function get()
	{
		global $config;
		switch($config['url_type']){
			case '1':
				$router = array('controller' => isset($_GET[$config['controller_trigger']]) ? trim($_GET[$config['controller_trigger']]) : '',
								'action' => isset($_GET[$config['method_trigger']]) ? trim($_GET[$config['method_trigger']]) : '',
								'directory' => isset($_GET[$config['directory_trigger']]) ? trim($_GET[$config['directory_trigger']]) : '');
				
			break;
			case '2':
				$path = trim(!empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '','/');
				$path = explode('/',$path);
				
				if($config['multi_mode']){
					$router = array('directory' => array_shift($path) ,'controller' => array_shift($path) ,'action' => array_shift($path));
				}else{
					$router = array('directory' => '','controller' => array_shift($path) ,'action' => array_shift($path));
				}
				
				if(count($path) > 1){
					foreach($path as $k){
						if(count($path) == 0) break;
						$_GET[array_shift($path)] = array_shift($path);
					}
					
				}
				$_POST = $_REQUEST = $_GET;
			break;
			default:
				$router = array('controller' => isset($_GET[$config['controller_trigger']]) ? trim($_GET[$config['controller_trigger']]) : '',
								'action' => isset($_GET[$config['method_trigger']]) ? trim($_GET[$config['method_trigger']]) : '',
								'directory' => isset($_GET[$config['directory_trigger']]) ? trim($_GET[$config['directory_trigger']]) : '');
			break;
		}
		$router['controller'] = empty($router['controller']) ? $config['default_controller'] : $router['controller'];
		$router['directory'] = $config['multi_mode'] == true ? (empty($router['directory']) ? 'index' : $router['directory']) : '';
		foreach($router as $k => $v){
			$router[$k] = strtolower($router[$k]);
			if(!preg_match("/^[a-z]+$/is",$v)){
				$router[$k] = $config['default_'.$k];
			}
		}
		
		return $router;

	}
}


?>